/*
First time? Check out the tutorial game:
https://sprig.hackclub.com/gallery/getting_started

@title: maze
@author: infinityloop
@tags: ["maze", "puzzle", "game"]
@description: a very generic maze puzzle game
@addedOn: 2025-20-07
*/

const player = "p"
const wall = "w"
const treasure = "t"
const enemy = "e"
const pushable = "u"
const bg = "z"
const switch1 = "x"
const switch2 = "y"
const door1 = "v"
const door2 = "q"
const portal = "l"
const portal2 = "m"
const end = "d"

setLegend(
  [player, bitmap`
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777
7777777777777777`],
  [wall, bitmap`
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000`],
  [treasure, bitmap`
6666666666666666
6666666666666666
6666666666666666
6666666666666666
6666666666666666
6666666666666666
6666666666666666
6666666666666666
6666666666666666
6666666666666666
6666666666666666
6666666666666666
6666666666666666
6666666666666666
6666666666666666
6666666666666666`],
  [enemy, bitmap`
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333`],
  [pushable, bitmap`
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL`],
  [bg, bitmap`
LLLLLLLLLLLLLLLL
L..............L
L..............L
L..............L
L..............L
L..............L
L..............L
L..............L
L..............L
L..............L
L..............L
L..............L
L..............L
L..............L
L..............L
LLLLLLLLLLLLLLLL`],
  [switch1, bitmap`
LLLLLLLLLLLLLLLL
L88888888888888L
L88888888888888L
L88888888888888L
L88888888888888L
L88888888888888L
L888888LL888888L
L88888LLLL88888L
L88888LLLL88888L
L888888LL888888L
L88888888888888L
L88888888888888L
L88888888888888L
L88888888888888L
L88888888888888L
LLLLLLLLLLLLLLLL`],
  [switch2, bitmap`
LLLLLLLLLLLLLLLL
LHHHHHHHHHHHHHHL
LHHHHHHHHHHHHHHL
LHHHHHHHHHHHHHHL
LHHHHHHHHHHHHHHL
LHHHHHHHHHHHHHHL
LHHHHHHLLHHHHHHL
LHHHHHLLLLHHHHHL
LHHHHHLLLLHHHHHL
LHHHHHHLLHHHHHHL
LHHHHHHHHHHHHHHL
LHHHHHHHHHHHHHHL
LHHHHHHHHHHHHHHL
LHHHHHHHHHHHHHHL
LHHHHHHHHHHHHHHL
LLLLLLLLLLLLLLLL`],
  [door1, bitmap`
8888888888888888
8888888888888888
8888888888888888
8888888888888888
8888888888888888
8888888888888888
8888888888888888
8888888888888888
8888888888888888
8888888888888888
8888888888888888
8888888888888888
8888888888888888
8888888888888888
8888888888888888
8888888888888888`],
  [door2, bitmap`
HHHHHHHHHHHHHHHH
HHHHHHHHHHHHHHHH
HHHHHHHHHHHHHHHH
HHHHHHHHHHHHHHHH
HHHHHHHHHHHHHHHH
HHHHHHHHHHHHHHHH
HHHHHHHHHHHHHHHH
HHHHHHHHHHHHHHHH
HHHHHHHHHHHHHHHH
HHHHHHHHHHHHHHHH
HHHHHHHHHHHHHHHH
HHHHHHHHHHHHHHHH
HHHHHHHHHHHHHHHH
HHHHHHHHHHHHHHHH
HHHHHHHHHHHHHHHH
HHHHHHHHHHHHHHHH`],
  [portal, bitmap`
7777777755555555
7777777755555555
7777777755555555
7777777755555555
7777777755555555
7777777755555555
7777777755555555
7777777755555555
5555555577777777
5555555577777777
5555555577777777
5555555577777777
5555555577777777
5555555577777777
5555555577777777
5555555577777777`],
  [portal2, bitmap`
5555555577777777
5555555577777777
5555555577777777
5555555577777777
5555555577777777
5555555577777777
5555555577777777
5555555577777777
7777777755555555
7777777755555555
7777777755555555
7777777755555555
7777777755555555
7777777755555555
7777777755555555
7777777755555555`],
  [end, bitmap`
3333333333333333
3999999999999993
3966666666666693
3964444444444693
3964DDDDDDDD4693
3964D777777D4693
3964D755557D4693
3964D758857D4693
3964D758857D4693
3964D755557D4693
3964D777777D4693
3964DDDDDDDD4693
3964444444444693
3966666666666693
3999999999999993
3333333333333333`],
)

setSolids([player, wall, pushable, door1, door2])

setBackground(bg)

let level = 0
const levels = [
  map`
........................................
.p......................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........................................
........w...............................
........w...............................
................u.......................
................u.......................
.........t..............................
.........t..............................
..........e.............................
..........e.............................
................x..............u........
................y..............u........
........................................
........................................
.......d................................
........................................`,
  map`
wwwwwww...wwwww...............w.........
wpwt.ww...w...wwwwwwww.wwwww..w.........
w.ww..w...w.w.w............w..w..www.w..
w.....w...w.w.wwwwwwwwwwww.ww.w..w...w..
wwwwwuww..w.w.wete.....w....w.w..w...w..
w......w..w.w.we.e.....w....w.wwwww..w..
w......w..w.w.we.e.....w....w.w......w..
w.x....w....w.w..e.....w..wuw........www
w......w....w.w.ee.....w..w.w.www.w.wetw
wwwwwvww....w.w........w..w.w.w.w.w.we.w
.......w..l.w.w.m......w..w.w.w.w.w.w..w
....ee.w....w.w........ww.w.w.w.w.w..w.w
....et.w....w.w........w..w.w.w.w.w....w
uuwwweew....w.w........w..w.w.w....w.w.w
w.w.wwwwwwwww.w........w..w.w.w....w.w.w
w..........ww.wwww.wwwww..w.w.w....w.w.w
w.wewwwwww.ww.w..w.w......w.w.wwww.w.www
wwwwwwwwww.ww.w..w.w......w.w....w.w...w
.....w.....ww.w..w.wwwwwwww.w....w..w.ww
...w....u..ww.w..w..........w....w..w..w
.y.....w...ww.w....wwwwwwwwwwwww.wuww.ww
.....w.....ww.w................w.w....ww
wwqwwwwwwwwww.wwwwwwwww.ww.....w.w....ww
.w.w........w.wtweew.....wwww..w.wwww.ww
.w.wwww.....w.w.wet....w.w..w..w.w..w..w
.w....w.....w.w.wee..w.w.w..w..w.w..w..w
.wwwuuw.....w.w.wwwwwwwwww..w.ww.w..w..w
...w...w....w.w.........wwwww.w..w..u..w
....w.w.....w.w....w...uu.....w..w..w..w
....w.wwwwwww.w....w....wwwwww...w..w..w
....w.........w..d.wwww.........ww..w..w
....w.wwwwwwwww.......wwwwwwwwwwwwwwwwww`,
  map`
p`,
]

setMap(levels[level])

setPushables({
  [player]: [pushable]
})

let coins = 0
let coinsCollectedInMap = 0
let door1x
let door1y
let door2x
let door2y
let portalx
let portaly

function resetMap() {
  //running
  if (coins <= 0) {
    setMap(levels[1])
    coins = 0
    coinsCollectedInMap = 0
  } else {
    coins--
    setMap(levels[level])
    for (i = 1; i < (coinsCollectedInMap + 1); i++) {
      getFirst(treasure).remove()
      //remove
    }
  }
  portalx = getFirst(portal).x
  portaly = getFirst(portal).y
}

onInput("w", () => {
  getFirst(player).y -= 1
})

onInput("s", () => {
  getFirst(player).y += 1
})

onInput("a", () => {
  getFirst(player).x -= 1
})

onInput("d", () => {
  getFirst(player).x += 1
})

onInput("k", () => {

  if (level == 0) {

    level = 1
    setMap(levels[1])
    clearText()
    portalx = getFirst(portal).x
    portaly = getFirst(portal).y
  } else {
    resetMap()
  }
})

onInput("l", () => {
  const playerSprite = getFirst(player)
  const playerX = playerSprite.x
  const playerY = playerSprite.y
  const currentBlockSprites = getTile(playerX, playerY)
  currentBlockSprites.forEach(sprite => {
    if (sprite.type == "t") {
      coins++
      coinsCollectedInMap++
      sprite.remove()
    }
  });
})

afterInput(() => {
  //switch 1 logic
  const switch1a = getFirst(switch1);
  if (switch1a && !level == 0) {
    const currentSwitch1Sprites = getTile(switch1a.x, switch1a.y);
    let switch1on = false
    currentSwitch1Sprites.forEach(sprite => {
      if (sprite.type == "u") {
        switch1on = true
        const door1Sprite = getFirst(door1);
        if (door1Sprite) {
          door1Sprite.remove();
        }
      }
      if (switch1on == false && !getFirst(door1)) {
        addSprite(door1x, door1y, door1)
      }
    });
  }
  //switch 2 logic
  const switch2a = getFirst(switch2);
  if (switch2a && !level == 0) {
    const currentSwitch2Sprites = getTile(switch2a.x, switch2a.y);
    let switch2on = false
    currentSwitch2Sprites.forEach(sprite => {
      if (sprite.type == "u") {
        switch2on = true
        const door2Sprite = getFirst(door2);
        if (door2Sprite) {
          door2Sprite.remove();
        }
      }
      if (switch2on == false && !getFirst(door2)) {
        addSprite(door2x, door2y, door2)
      }
    });
  }

  const playerSprite = getFirst(player)
  const playerX = playerSprite.x
  const playerY = playerSprite.y
  const currentBlockSprites = getTile(playerX, playerY)
  currentBlockSprites.forEach(sprite => {
    if (sprite.type == "e") {
      resetMap()
    }

    if (sprite.type == "l") {
      getFirst(player).x = getFirst(portal2).x
      getFirst(player).y = getFirst(portal2).y
    }
    if (sprite.type == "m") {
      getFirst(player).x = getFirst(portal).x
      getFirst(player).y = getFirst(portal).y
    }

    if (sprite.type == "d") {
      level++
      setMap(levels[level])
    }
  });

  if (level !== 0) {
    clearText()
    addText(coins.toString(), {
      x: 0,
      y: 0,
      color: color`6`
    })
  }

})


addText("WASD - Move", {
  x: 0,
  y: 0,
  color: color`0`
})
addText("I - Attack", {
  x: 0,
  y: 1,
  color: color`0`
})
addText("L - Collect", {
  x: 0,
  y: 2,
  color: color`0`
})
addText("K - Reset", {
  x: 0,
  y: 3,
  color: color`0`
})
addText("Wall", {
  x: 0,
  y: 9,
  color: color`0`
})

addText("Pushable - Push", {
  x: 0,
  y: 10,
  color: color`0`
})

addText("Gold - Collect", {
  x: 0,
  y: 11,
  color: color`0`
})

addText("Enemy - Avoid", {
  x: 0,
  y: 12,
  color: color`0`
})
addText("Switches - Push  on", {
  x: 0,
  y: 13,
  color: color`0`
})

addText("End - Press to start", {
  x: 0,
  y: 15,
  color: color`0`
})

const arp = tune`
230.76923076923077: A4^230.76923076923077,
230.76923076923077: C5^230.76923076923077,
230.76923076923077: E5^230.76923076923077,
230.76923076923077: C5^230.76923076923077,
230.76923076923077: A4^230.76923076923077,
230.76923076923077: C5^230.76923076923077,
230.76923076923077: E5^230.76923076923077,
230.76923076923077: C5^230.76923076923077,
230.76923076923077: G4^230.76923076923077,
230.76923076923077: C5^230.76923076923077,
230.76923076923077: E5^230.76923076923077,
230.76923076923077: C5^230.76923076923077,
230.76923076923077: G4^230.76923076923077,
230.76923076923077: C5^230.76923076923077,
230.76923076923077: E5^230.76923076923077,
230.76923076923077: C5^230.76923076923077,
230.76923076923077: F4^230.76923076923077,
230.76923076923077: C5^230.76923076923077,
230.76923076923077: E5^230.76923076923077,
230.76923076923077: C5^230.76923076923077,
230.76923076923077: F4^230.76923076923077,
230.76923076923077: C5^230.76923076923077,
230.76923076923077: E5^230.76923076923077,
230.76923076923077: C5^230.76923076923077,
230.76923076923077: G4^230.76923076923077,
230.76923076923077: C5^230.76923076923077,
230.76923076923077: E5^230.76923076923077,
230.76923076923077: C5^230.76923076923077,
230.76923076923077: G4^230.76923076923077,
230.76923076923077: C5^230.76923076923077,
230.76923076923077: E5^230.76923076923077,
230.76923076923077: C5^230.76923076923077`
const chords = tune`
230.76923076923077: A4^230.76923076923077 + C5^230.76923076923077 + E5^230.76923076923077,
230.76923076923077,
230.76923076923077: E5^230.76923076923077 + A4^230.76923076923077 + C5^230.76923076923077,
230.76923076923077,
230.76923076923077: A4^230.76923076923077 + C5^230.76923076923077 + E5^230.76923076923077,
230.76923076923077,
230.76923076923077: E5^230.76923076923077 + C5^230.76923076923077 + A4^230.76923076923077,
230.76923076923077,
230.76923076923077: E5^230.76923076923077 + C5^230.76923076923077 + G4^230.76923076923077,
230.76923076923077,
230.76923076923077: E5^230.76923076923077 + C5^230.76923076923077 + G4^230.76923076923077,
230.76923076923077,
230.76923076923077: C5^230.76923076923077 + E5^230.76923076923077 + G4^230.76923076923077,
230.76923076923077,
230.76923076923077: E5^230.76923076923077 + C5^230.76923076923077 + G4^230.76923076923077,
230.76923076923077,
230.76923076923077: E5^230.76923076923077 + C5^230.76923076923077 + F4^230.76923076923077,
230.76923076923077,
230.76923076923077: E5^230.76923076923077 + C5^230.76923076923077 + F4^230.76923076923077,
230.76923076923077,
230.76923076923077: E5^230.76923076923077 + C5^230.76923076923077 + F4^230.76923076923077,
230.76923076923077,
230.76923076923077: E5^230.76923076923077 + C5^230.76923076923077 + F4^230.76923076923077,
230.76923076923077,
230.76923076923077: E5^230.76923076923077 + C5^230.76923076923077 + G4^230.76923076923077,
230.76923076923077,
230.76923076923077: E5^230.76923076923077 + C5^230.76923076923077 + G4^230.76923076923077,
230.76923076923077,
230.76923076923077: C5^230.76923076923077 + E5^230.76923076923077 + G4^230.76923076923077,
230.76923076923077,
230.76923076923077: E5^230.76923076923077 + C5^230.76923076923077 + G4^230.76923076923077,
230.76923076923077`
const bass = tune`
230.76923076923077: A4-230.76923076923077,
230.76923076923077: A4-230.76923076923077,
230.76923076923077: A4-230.76923076923077,
230.76923076923077: A4-230.76923076923077,
230.76923076923077: A4-230.76923076923077,
230.76923076923077: A4-230.76923076923077,
230.76923076923077: A4-230.76923076923077,
230.76923076923077: C5-230.76923076923077,
230.76923076923077: G4-230.76923076923077,
230.76923076923077: G4-230.76923076923077,
230.76923076923077: G4-230.76923076923077,
230.76923076923077: G4-230.76923076923077,
230.76923076923077: G4-230.76923076923077,
230.76923076923077: G4-230.76923076923077,
230.76923076923077: G4-230.76923076923077,
230.76923076923077: C5-230.76923076923077,
230.76923076923077: F4-230.76923076923077,
230.76923076923077: F4-230.76923076923077,
230.76923076923077: F4-230.76923076923077,
230.76923076923077: F4-230.76923076923077,
230.76923076923077: F4-230.76923076923077,
230.76923076923077: F4-230.76923076923077,
230.76923076923077: F4-230.76923076923077,
230.76923076923077: C5-230.76923076923077,
230.76923076923077: G4-230.76923076923077,
230.76923076923077: G4-230.76923076923077,
230.76923076923077: G4-230.76923076923077,
230.76923076923077: G4-230.76923076923077,
230.76923076923077: G4-230.76923076923077,
230.76923076923077: G4-230.76923076923077,
230.76923076923077: G4-230.76923076923077,
230.76923076923077: C5-230.76923076923077`
const melody = tune`
230.76923076923077: A4/230.76923076923077,
230.76923076923077: C5/230.76923076923077,
230.76923076923077: E5/230.76923076923077,
230.76923076923077: A4/230.76923076923077,
230.76923076923077,
230.76923076923077: E4/230.76923076923077,
230.76923076923077: A4/230.76923076923077,
230.76923076923077: A4/230.76923076923077,
230.76923076923077: G4/230.76923076923077,
230.76923076923077: G4/230.76923076923077,
230.76923076923077,
230.76923076923077: C5/230.76923076923077,
230.76923076923077: E5/230.76923076923077,
230.76923076923077: A4/230.76923076923077,
230.76923076923077: G4/230.76923076923077,
230.76923076923077: A4/230.76923076923077,
230.76923076923077: F4/230.76923076923077,
230.76923076923077: F4/230.76923076923077,
230.76923076923077: A4/230.76923076923077,
230.76923076923077: F4/230.76923076923077,
230.76923076923077: C5/230.76923076923077,
230.76923076923077: B4/230.76923076923077,
230.76923076923077: B4/230.76923076923077,
230.76923076923077: C5/230.76923076923077,
230.76923076923077: D5/230.76923076923077,
230.76923076923077: D5/230.76923076923077,
230.76923076923077: B4/230.76923076923077,
230.76923076923077: G4/230.76923076923077,
230.76923076923077: B4/230.76923076923077,
230.76923076923077: G4/230.76923076923077,
461.53846153846155`

playTune(arp, Infinity)
playTune(chords, Infinity)
playTune(bass, Infinity)
playTune(melody, Infinity)
